from manim import *
class CustomCurve(VMobject):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        # 定义自定义曲线的控制点
        points = [
            [15, 10, 0],
            [10,5,0],
            [15,0,0]
            
        ]
        self.set_points_smoothly(points)


class math1(MovingCameraScene):
    def construct(self):
        self.camera.background_color = BLACK  # 设置背景颜色
        self.camera.frame_width = 100  # 设置边框宽度
        self.camera.frame_height = 60  # 设置边框高度
        self.camera.pixel_height = 1080  # 设置像素高度
        self.camera.pixel_width = 1920  # 设置像素宽度
        self.camera.center = ORIGIN  # 设置中心点位置
        self.camera.scale_factor = 1.0  # 设置缩放因子
        #设置横线
        for i in range(60*2+1):       
            dot1=Dot([-500,5*(i-60),0]).set_opacity(0.5)
            dot2=Dot([500,5*(i-60),0]).set_opacity(0.5)
            if i==6:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
                
            else:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
               
            self.add(dot1,dot2,line1)
        #设置竖线
        for i in range(100*2+1):        
            dot3=Dot([(i-100)*5,-300,0]).set_opacity(0.5)
            dot4=Dot([(i-100)*5,300,0]).set_opacity(0.5)
            if i==10:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
                
            else:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
            self.add(dot3,dot4,line2)
        #设置三个点
        dot1 = Dot(radius=1, color=RED)  
        dot1.move_to([-47.5,27.5,0]) 
        dot2 = Dot(radius=1, color=YELLOW)  
        dot2.move_to([-42.5,27.5,0])  
        dot3 = Dot(radius=1, color=GREEN)  
        dot3.move_to([-37.5,27.5,0])
        
        #镜头跟进效果 
        self.camera.frame.save_state()
        #题目出现
        text = Tex(r"\text{(张1000题2)求函数}$f\left( x,y\right) =\dfrac{x+y}{\left( 1+x^{2}\right) \left( 1+y^{2}\right) }$\text{的极值}"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,22,0])
        
        
        self.play(Write(text))
        
        #镜头拉回
          #分析文本
        #text100=MarkupText("<b><i>分析/解：</i></b>",color=PINK).scale(5.0).move_to([-38,12.5,0])
        
        self.play(FadeIn(dot1),run_time=0.1)
        self.play(FadeIn(dot2),run_time=0.1)
        self.play(FadeIn(dot3),run_time=0.1)

        text30 = Tex(
                   r"$\dfrac{\partial f}{\partial x}=$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-40,10,0])
        text3 = Tex(
                   r"$\dfrac{1-x^{2}-2xy}{\left( 1+x^{2}\right) ^{2}\left( 1+y^{2}\right) }$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text30,RIGHT,buff=1)
        self.play(Write(text30))
        self.play(Write(text3))
        text4 = Tex(r"$f\left( y,x\right) =\dfrac{y+x}{\left( 1+y^{2}\right) \left( 1+x^{2}\right) }$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([8,15,0])
        self.play(Write(text4))
        #框框
        rectangle=Rectangle(color=BLUE,fill_opacity=0.0,width=20,height=14
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle.move_to([15, 17.5, 0])
        self.play(Create(rectangle))
        
        text50 = Tex(
                   r"$\dfrac{\partial f}{\partial y}=$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-40,0,0])
        text5 = Tex(
                   r"$\dfrac{1-y^{2}-2xy}{\left( 1+y^{2}\right) ^{2}\left( 1+x^{2}\right) }$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text50,RIGHT,buff=1)
        self.play(Write(text50))
        self.play(Write(text5))

        self.play(FadeOut(rectangle),FadeOut(text4))

        text6 = Tex(
                   r"$=0$",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text3,RIGHT,buff=1)
        text7 = Tex(
                   r"$=0$",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text5,RIGHT,buff=1)
        self.play(Write(text6),Write(text7))

        #箭头引出
        arrow = Arrow(start=[-8,5,0],end=[0,5,0], color=RED
                       , stroke_width=20, tip_length=1)
        self.play(Create(arrow))

        text8 = Tex(
                   r"$x^{2}=y^{2}$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([5,5,0])
        self.play(Write(text8))

        curve = CustomCurve(color=GREEN, stroke_width=20)
        
        # 添加到场景并播放动画
        self.play(Create(curve))
        self.wait(0.3)

        text9 = Tex(
                   r"$x=y$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([20,10,0])
        text10 = Tex(
                   r"$x=-y$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([20,0,0])
        self.play(Write(text9),Write(text10))

        #框框
        rectangle1=Rectangle(color=BLUE,fill_opacity=0.0,width=10,height=5
                            ,stroke_color=YELLOW,stroke_width=20)
        rectangle1.move_to([20,10,0])
        self.play(Create(rectangle1))

        rectangle2=Rectangle(color=BLUE,fill_opacity=0.0,width=35,height=10
                            ,stroke_color=YELLOW,stroke_width=20)
        rectangle2.move_to([-28,10,0])
        self.play(
            ReplacementTransform(rectangle1,rectangle2)
        )
        self.wait(0.5)
        
        text11 = Tex(
                   r"$1-3x^{2}=0$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,15,0])
        self.play(
            ReplacementTransform(rectangle2,text11)
        )
        self.wait(0.5)
        text12 = Tex(
                   r"$x=\pm \dfrac{\sqrt{3}}{3}$",color=PURPLE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,15,0])
        self.play(
            ReplacementTransform(text11,text12)
        )
        self.wait(0.5)
        text13 = Tex(
                   r"$\left( \dfrac{\sqrt{3}}{3},\dfrac{\sqrt{3}}{3}\right) ,\left( -\dfrac{\sqrt{3}}{3},-\dfrac{\sqrt{3}}{3}\right) $",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(4.0).next_to(text9,RIGHT,buff=1)
        self.play(
            ReplacementTransform(text12,text13)
        )
        self.wait(0.5)



        ###########################################################################






        #框框
        rectangle1=Rectangle(color=BLUE,fill_opacity=0.0,width=10,height=5
                            ,stroke_color=YELLOW,stroke_width=20)
        rectangle1.move_to([20,0,0])
        self.play(Create(rectangle1))

        rectangle2=Rectangle(color=BLUE,fill_opacity=0.0,width=35,height=10
                            ,stroke_color=YELLOW,stroke_width=20)
        rectangle2.move_to([-28,10,0])
        self.play(
            ReplacementTransform(rectangle1,rectangle2)
        )
        self.wait(0.5)
        
        text11 = Tex(
                   r"$1+x^{2}=0$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,15,0])
        self.play(
            ReplacementTransform(rectangle2,text11)
        )
        self.wait(0.5)
        text12 = Tex(
                   r"\text{(矛盾)}",color=PURE_RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text11,RIGHT,buff=2)
        self.play(
            Write(text12)
        )
        self.wait(0.5)
        text13 = Tex(
                   r"\text(舍去)",color=PURE_RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text10,RIGHT,buff=5)
        self.play(
            ReplacementTransform(text12,text13)
            ,ReplacementTransform(text11,text13)
        )
        self.wait(0.5)

        text14 = Tex(
                   r"$\dfrac{\partial ^{2}f}{\partial x^{2}}=\dfrac{\left( 1-x^{2}-2xy\right) ^{'}\left( 1+x^{2}\right) ^{2}\left( 1+y^{2}\right) -\left( 1-x^{2}-2xy\right) \left[ \left( 1+x^{2}\right) ^{2}\left( 1+y^{2}\right) \right] ^{'}}{\left( 1+x^{2}\right) ^{4}\left( 1+y^{2}\right) ^{2}}$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-10,0])
        self.play(Write(text14))


         #半透明矩阵扩展
        rectangle4=Rectangle(color=RED,fill_opacity=0.5,width=1,height=2.8
                            ,stroke_color=YELLOW,stroke_width=0).move_to([9,-11.3,0])
        self.play(
            rectangle4.animate.stretch(16.0,dim=0)
        )
        rectangle5=Rectangle(color=RED,fill_opacity=0.5,width=40,height=2.8
                            ,stroke_color=YELLOW,stroke_width=0).move_to([20,-11.3,0])
        
        self.play(
            ReplacementTransform(rectangle4,rectangle5)
            
        )
        self.wait(0.5)
        text15 = Tex(
                   r"$0$",color=PURE_GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(8.0).next_to(rectangle5,UP,buff=1)
        self.play(Write(text15))

        #半透明矩阵扩展
        rectangle6=Rectangle(color=GREEN,fill_opacity=0.3,width=1,height=2.8
                            ,stroke_color=YELLOW,stroke_width=0).move_to([-12,-11.3,0])
        rectangle7=Rectangle(color=GREEN,fill_opacity=0.3,width=1,height=2.8
                            ,stroke_color=YELLOW,stroke_width=0).move_to([0,-16.5,0])
        rectangle8=Rectangle(color=BLUE,fill_opacity=0.3,width=1,height=2.8
                            ,stroke_color=YELLOW,stroke_width=0).move_to([-32,-11.3,0])
        self.play(
            rectangle6.animate.stretch(16.0,dim=0)
            ,rectangle7.animate.stretch(20.0,dim=0)
        )
        self.play(
            rectangle8.animate.stretch(16.0,dim=0)
            
        )

        text16 = Tex(
                   r"$ A=\dfrac{\partial ^{2}f}{\partial x^{2}}| \left( \frac{\sqrt{3}}{3},\frac{\sqrt{3}}{3}\right) = \dfrac{-2\left( x+y\right) }{\left( 1+x^{2}\right) ^{2}\left( 1+y^{2}\right) }|\left( \frac{\sqrt{3}}{3},\frac{\sqrt{3}}{3}\right) $",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-10,-25,0])
        self.play(Write(text16),self.camera.frame.animate.scale(1).shift(5*DOWN))

        text17 = Tex(
                   r"$=-\dfrac{9\sqrt{3}}{16}$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text16,RIGHT,buff=1)
        self.play(Write(text17),self.camera.frame.animate.scale(1).shift(5*DOWN))

        text18 = Tex(
                   r"$ C=\dfrac{\partial ^{2}f}{\partial y^{2}}| \left( \frac{\sqrt{3}}{3},\frac{\sqrt{3}}{3}\right) = \dfrac{-2\left( x+y\right) }{\left( 1+y^{2}\right) ^{2}\left( 1+x^{2}\right) }|\left( \frac{\sqrt{3}}{3},\frac{\sqrt{3}}{3}\right) $",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-10,-35,0])
        self.play(Write(text18),self.camera.frame.animate.scale(1).shift(5*DOWN))

        text19 = Tex(
                   r"$=-\dfrac{9\sqrt{3}}{16}$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text18,RIGHT,buff=1)
        self.play(Write(text19),self.camera.frame.animate.scale(1).shift(5*DOWN))

        #####################
        self.play(FadeOut(text14),FadeOut(text15),FadeOut(rectangle5),FadeOut(rectangle6),FadeOut(rectangle7),FadeOut(rectangle8))
        text20 = Tex(
                   r"$\dfrac{\partial ^{2}f}{\partial x\partial y}=\dfrac{\left( 1-x^{2}-2xy\right) ^{'}\left( 1+x^{2}\right) ^{2}\left( 1+y^{2}\right) -\left( 1-x^{2}-2xy\right) \left[ \left( 1+x^{2}\right) ^{2}\left( 1+y^{2}\right) \right] ^{'}}{\left( 1+x^{2}\right) ^{4}\left( 1+y^{2}\right) ^{2}}$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-10,0])
        self.play(Write(text20))
        rectangle9=Rectangle(color=BLUE,fill_opacity=0.3,width=1,height=2.8
                            ,stroke_color=YELLOW,stroke_width=0).move_to([-32,-11.3,0])
        rectangle10=Rectangle(color=BLUE,fill_opacity=0.3,width=1,height=2.8
                            ,stroke_color=YELLOW,stroke_width=0).move_to([9,-11.3,0])
        self.play(
            rectangle9.animate.stretch(16.0,dim=0)
            ,rectangle10.animate.stretch(20.0,dim=0)
        )

        text21 = Tex(
                   r"$ B=\dfrac{\partial ^{2}f}{\partial x\partial y}| \left( \frac{\sqrt{3}}{3},\frac{\sqrt{3}}{3}\right) = \dfrac{-2x}{\left( 1+x^{2}\right) ^{2}\left( 1+y^{2}\right) }|\left( \frac{\sqrt{3}}{3},\frac{\sqrt{3}}{3}\right) $",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-10,-45,0])
        self.play(Write(text21),self.camera.frame.animate.scale(1).shift(5*DOWN))

        text22 = Tex(
                   r"$=-\dfrac{9\sqrt{3}}{32}$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text21,RIGHT,buff=1)
        self.play(Write(text22),self.camera.frame.animate.scale(1).shift(5*DOWN))

        text23 = Tex(
                   r"$AC-B^{2}=\left( \dfrac{-9}{16}\sqrt{3}\right) ^{2}-\left( \dfrac{9}{32}\sqrt{3}\right) ^{2}$",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-10,-55,0])
        self.play(Write(text23),self.camera.frame.animate.scale(1).shift(5*DOWN))
        text24 = Tex(
                   r"$ >0$\text{且}$A<0$",color=PURE_RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text23,RIGHT,buff=1)
        self.play(Write(text24))
        text25 = Tex(
                   r"$\left( \dfrac{\sqrt{3}}{3},\dfrac{\sqrt{3}}{3}\right) $\text{为极大值点}",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-20,-65,0])
        self.play(Write(text25),self.camera.frame.animate.scale(1).shift(5*DOWN))

        text26 = Tex(
                   r"\text{,那么极大值为}$f\left( \dfrac{\sqrt{3}}{3},\dfrac{\sqrt{3}}{3}\right) =\dfrac{3\sqrt{3}}{8}$",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text25,RIGHT,buff=1)
        self.play(Write(text26),self.camera.frame.animate.scale(1).shift(5*DOWN))
        rectangle11=Rectangle(color=BLUE,fill_opacity=0.0,width=10,height=2.8
                            ,stroke_color=RED,stroke_width=20).move_to([-22.5,-25,0])
        rectangle12=Rectangle(color=BLUE,fill_opacity=0.0,width=10,height=2.8
                            ,stroke_color=RED,stroke_width=20).move_to([14,-25,0])
        rectangle13=Rectangle(color=BLUE,fill_opacity=0.0,width=10,height=2.8
                            ,stroke_color=RED,stroke_width=20).move_to([-20,-35,0])
        rectangle14=Rectangle(color=BLUE,fill_opacity=0.0,width=10,height=2.8
                            ,stroke_color=RED,stroke_width=20).move_to([16,-35,0])
        rectangle15=Rectangle(color=BLUE,fill_opacity=0.0,width=14,height=2.8
                            ,stroke_color=RED,stroke_width=20).move_to([-20,-45,0])
        rectangle16=Rectangle(color=BLUE,fill_opacity=0.0,width=14,height=2.8
                            ,stroke_color=RED,stroke_width=20).move_to([16,-45,0])
        self.play(Create(rectangle11),Create(rectangle12),Create(rectangle13)
                  ,Create(rectangle14),Create(rectangle15),Create(rectangle16))
        text220 = Tex(
                   r"$=\dfrac{9\sqrt{3}}{32}$",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text22,RIGHT,buff=1)
        text190 = Tex(
                   r"$=\dfrac{9\sqrt{3}}{16}$",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text19,RIGHT,buff=1)
        text170 = Tex(
                   r"$=\dfrac{9\sqrt{3}}{16}$",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text17,RIGHT,buff=1)
        self.play(Create(text220),Create(text190),Create(text170))
        text29 = Tex(
                   r"$AC-B^{2}=\left( \dfrac{-9}{16}\sqrt{3}\right) ^{2}-\left( \dfrac{9}{32}\sqrt{3}\right) ^{2}$",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-10,-75,0])
        self.play(Write(text29),self.camera.frame.animate.scale(1).shift(5*DOWN))
        text30 = Tex(
                   r"$ >0$\text{且}$A>0$",color=PURE_RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text29,RIGHT,buff=1)
        self.play(Write(text30),self.camera.frame.animate.scale(1).shift(5*DOWN))
        text27 = Tex(
                   r"$\left( -\dfrac{\sqrt{3}}{3},-\dfrac{\sqrt{3}}{3}\right) $\text{为极小值点}",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-20,-85,0])
        self.play(Write(text27),self.camera.frame.animate.scale(1).shift(5*DOWN))

        text28 = Tex(
                   r"\text{,那么极小值为}$f\left( -\dfrac{\sqrt{3}}{3},-\dfrac{\sqrt{3}}{3}\right) =-\dfrac{3\sqrt{3}}{8}$",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text27,RIGHT,buff=1)
        self.play(Write(text28),self.camera.frame.animate.scale(1).shift(5*DOWN))
        self.wait(0.3)
        self.play(self.camera.frame.animate.scale(2).move_to([0,-30,0]))
        self.wait()